
/**
 * 验证手机号
 */
export function validPhone(phoneStr: string): boolean {
  const mobile = /^((13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])+\d{8})$/
  return mobile.test(phoneStr)
}

/**
 * 验证身份证
 */
export function validIdCard(idCardStr: string): boolean {
  const idCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/
  return idCard.test(idCardStr)
}

/**
 * 验证邮箱
 */
export function validEmail(emailStr: string): boolean {
  const email = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
  return email.test(emailStr)
}

/** 验证两个map的key是否相等 */
export function areKeysEqual(map1: Map<any, any>, map2: Map<any, any>) {
  const keys1 = Array.from(map1.keys())
  const keys2 = Array.from(map2.keys())

  // 判断键集合是否相等
  if (keys1.length !== keys2.length)
    return false

  for (let i = 0; i < keys1.length; i++) {
    if (!keys2.includes(keys1[i]))
      return false
  }

  return true
}

/** 每个键对应的值是否相等 */
export function areValuesEqual(map1: Map<any, any>, map2: Map<any, any>) {
  const keys1 = Array.from(map1.keys())

  // 检查每个键对应的值是否相等
  for (let i = 0; i < keys1.length; i++) {
    const key = keys1[i]
    if (!map2.has(key) || !deepEqual(map1.get(key), map2.get(key)))
      return false
  }

  return true
}

export function deepEqual(value1: any, value2: any) {
  if (value1 === value2)
    return true

  if (typeof value1 !== 'object' || typeof value2 !== 'object' || value1 === null || value2 === null)
    return false

  const props1 = Object.keys(value1)
  const props2 = Object.keys(value2)

  if (props1.length !== props2.length)
    return false

  for (let i = 0; i < props1.length; i++) {
    const prop = props1[i]

    // eslint-disable-next-line no-prototype-builtins
    if (!value2.hasOwnProperty(prop) || !deepEqual(value1[prop], value2[prop]))
      return false
  }

  return true
}
